module V{
    /** 游戏界面 */
    export class GameView extends eui.Component{
        /** 玩家列表 */
        public playerList:PlayerView[];
        /** 玩家数量 */
        public MAX_PLAYER:number;
        /** 单元积分 */
        public cellScore:number;
        /** 资源组名字 */
        public Res:string[];

        public constructor(){
            super();
            this.Res = [];
            this.playerList = [];
            D.App.gameView = this;
            this.percentHeight = this.percentWidth = 100;
            this.cellScore = D.App.roomData.cellScore;
        }
        /** 子元素创建完成后 */
        protected childrenCreated():void{
            this.initPlayer();
            if(this["menuView"]){ //通用菜单
                if(this["btMenu"]) this["btMenu"].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onMenuButton, this);
                if(this["btExit"]) this["btExit"].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBackButton, this);
                if(this["btHelp"]) this["btHelp"].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onHelpButton, this);
                this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBgButton, this);
            }
        }
        /** 收到网络消息 */
        public onNetData(s:number, d:any):void{}
        /** 同步桌子 */
        public onSyncData(d:any):void{}
        /** 释放对象 */
        public onDestroy(): void {
            if(this["menuView"]){ //通用菜单
                if(this["btMenu"]) this["btMenu"].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onMenuButton, this);
                if(this["btExit"]) this["btExit"].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onBackButton, this);
                if(this["btHelp"]) this["btHelp"].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onHelpButton, this);
                this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onBgButton, this);
            }
            egret.Tween.removeAllTweens();
            this.parent.removeChild(this);
        }

        /** 显示玩家 */
        public showPlayer(c:number):void{ //console.log("showPlayer",c);
            let cid = c;
            c = this.c2p(c); //console.log("2showPlayer",c);
            if(this.resetChair() || c == 65535){
                for(let i=0;i<this.MAX_PLAYER;i++){
                    this.playerList[i].chairId = this.c2p(i);
                    this.playerList[i].show();
                }
            }else if(this.playerList[c]){
                this.playerList[c].chairId = cid;
                this.playerList[c].show();
            } 
        }
        /** 根据位置获取椅子号 此方法要在所有元素创建完了调用就是childrenCreated */
        public initPlayer():void{
            this.playerList = [];
            for(let i=0;i<this.MAX_PLAYER;i++){
                if(this["player" + i]){
                    this.playerList[i] = this["player" + i];
                    this.playerList[i].init();
                    this.playerList[i].chairId = 65535;
                }
            }
        }
        /** 重置椅子 */
        public resetChair():boolean{
            if(this.playerList[0] && this.playerList[0].chairId == D.App.userData.chairID) return false;
            for(let i=0;i<this.MAX_PLAYER;i++){
                if(this.playerList[i]){
                    if(D.App.userData.chairID == 65535) this.playerList[i].chairId = i;
                    else this.playerList[i].chairId = (i+D.App.userData.chairID)%this.MAX_PLAYER;
                }
            }
            return true;
        }
        /** 椅子号转位置 */
        public c2p(c):number{
            if(c == 65535 || D.App.userData.chairID == 65535) return c;
            if(c < D.App.userData.chairID) c = this.MAX_PLAYER - (D.App.userData.chairID - c);
            else c = c - D.App.userData.chairID;
            return c;
        }
        /** 位置转椅子号 */
        public p2c(s):number{
            if(D.App.userData.chairID == 65535) return s;
            else s += D.App.userData.chairID;
            if(s >= this.MAX_PLAYER) s -= this.MAX_PLAYER;
            return s;
        }
        /** 获取上个玩家 */
        public getPre(c:number):number{
            for(let i=0;i<this.MAX_PLAYER;i++){
                c--;
                if(c < 0) c = this.MAX_PLAYER - 1;
                if(D.App.table.players[c]) return c;
            }
        }
        /** 根据椅子号获取玩家数据 */
        public getPlayerData(c:number):D.PlayerData
        {
            for(var i = 0; i < this.playerList.length; ++i)
            {
                if(this.playerList[i].chairId == c)
                {
                    return this.playerList[i].getData();
                }
            }
            return null;
        }
        /** 按下菜单按钮 */
        public onMenuButton(e:egret.TouchEvent):void{
            this["menuView"].visible = !this["menuView"].visible;
            e.stopPropagation();
        }
        /** 点击了返回按钮 */
        public onBackButton(e:egret.TouchEvent):void{
            this["menuView"].visible = false;
            if (D.App.userData.userStatus == UserStatus.PLAY && this.playerList[this.c2p(D.App.userData.chairID)].isPlay()) {
                V.Dialog.show("确定要退出游戏吗？", function(){
                    D.App.net.send([8,4]);
                }, function () {}, this);
            } else {
                D.App.net.send([8,4]);
            }
        }
        /** 点击了帮助按钮 */
        public onHelpButton(e:egret.TouchEvent):void{}
        /** 背景被点击 */
        public onBgButton(e:egret.TouchEvent):void{
            this["menuView"].visible = false;
        }
        /** 退出房间 */
        public onExit(msg:string = ""):void{
            
        }
    }
}